﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：Configuration_ModuleFunctionViewModel
// 创 建 者：杨程
// 创建时间：2021/10/14 17:31:25
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using V.CodeGenerator.CreateCodes.Model;
using Vampirewal.Core;
using Vampirewal.Core.Extensions;
using Vampirewal.Core.Interface;
using Vampirewal.Core.Models;
using Vampirewal.Core.IoC;
using Vampirewal.Core.SimpleMVVM;
using V.CodeGenerator.Model;
using System.Collections.ObjectModel;
using System.Reflection;

namespace V.CodeGenerator.ViewModel
{
    [VampirewalIoCRegister(ViewModelKey.Configuration_ModuleFunctionViewModel, RegisterType.ViewModel)]
    public class Configuration_ModuleFunctionViewModel : BillVM<ProjectPoco>
    {
        private IDialogMessage Dialog { get; set; }
        private IValidationService ValidationService { get; set; }
        public Configuration_ModuleFunctionViewModel(IDataContext dc, IDialogMessage dialog,IValidationService validationService) : base(dc)
        {
            //构造函数
            Dialog = dialog;
            ValidationService = validationService;


        }

        #region 重写
        public override void BillVmInitData()
        {
            Modules = new ObservableCollection<ModulePoco>();

            Functions = new ObservableCollection<FunctionPoco>();
        }

        public override void InitData()
        {
            //ModuleFunctions = EnumHelper.EnumToList<ModuleFunctionType>().Where(e => e.Classification >= 1 && e.Classification <= 2).OrderBy(e => e.Classification).ToList();

            
        }
        public override void PassData(object obj)
        {
            SetEntity(obj as ProjectPoco);

            LoadData();
        }

        

        public RelayCommand SaveCommand => new RelayCommand(() =>
        {
            //if (TreeSelectItem.moduleFunctionType== ModuleFunctionType.Top)
            //{
            //    Dialog.ShowPopupWindow("顶层不能修改！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);

            //    return;
            //}

            //try
            //{
            //    DC.Client.BeginTran();

            //    DC.UpdateEntity(TreeSelectItem);

            //    Dialog.ShowPopupWindow("保存成功！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Successful);

            //    DC.Client.CommitTran();

            //    TreeSelectItem = null;
            //}
            //catch
            //{
            //    DC.Client.RollbackTran();
            //}
        });

        
        #endregion

        #region 属性
        //private List<EnumberCreditType> _ModuleFunctions;
        //public List<EnumberCreditType> ModuleFunctions { get => _ModuleFunctions; set { _ModuleFunctions = value; DoNotify(); } }

        public ObservableCollection<ModulePoco> Modules { get; set; }
        public ObservableCollection<FunctionPoco> Functions { get; set; }


        #region 弹窗的model
        private ModulePoco _CurrentModuleFunctionModel;

        public ModulePoco CurrentModuleModel
        {
            get { return _CurrentModuleFunctionModel; }
            set { _CurrentModuleFunctionModel = value; DoNotify(); }
        }


        private FunctionPoco _CurrentFunctionModel;

        public FunctionPoco CurrentFunctionModel
        {
            get { return _CurrentFunctionModel; }
            set { _CurrentFunctionModel = value; DoNotify(); }
        }

        #endregion

        private bool _Show;

        public bool Show
        {
            get { return _Show; }
            set { _Show = value; DoNotify(); }
        }


        private ModulePoco _SelectedModule;

        public ModulePoco SelectedModule
        {
            get { return _SelectedModule; }
            set { _SelectedModule = value; DoNotify(); }
        }

        private bool _Show2;

        public bool Show2
        {
            get { return _Show2; }
            set { _Show2 = value; DoNotify(); }
        }

        private FunctionPoco _SelectedFunction;

        public FunctionPoco SelectedFunction
        {
            get { return _SelectedFunction; }
            set { _SelectedFunction = value; DoNotify(); }
        }

        #endregion

        #region 公共方法

        #endregion

        #region 私有方法
        private void LoadData()
        {
            Modules.Clear();

            var CurModules = DC.Client.Queryable<ModulePoco>().Where(w => w.ProjectId == Entity.BillId).ToList();
            CurModules.ForEach(f => Modules.Add(f));
        }
        #endregion

        #region 命令

        #region 左侧模块
        public RelayCommand<ModulePoco> SelectTreeItemsCommand => new RelayCommand<ModulePoco>((s) =>
        {
            SelectedModule = s;

            Functions.Clear();

            var currentFunctions= DC.Client.Queryable<FunctionPoco>().Where(w => w.BillId == s.BillId && w.ProjectId == s.ProjectId).ToList();

            currentFunctions.ForEach(f => Functions.Add(f));
        });

        public RelayCommand CreateModuleFunctionModelCommand => new RelayCommand(() =>
        {

            CurrentModuleModel = new ModulePoco()
            {
                ProjectId = Entity.BillId,
                Sort = Modules.Count,
                CreateBy = "admin",
                CreateTime = DateTime.Now
            };

            Show = true;


            //ModuleFunctions.Clear();
            //ModuleFunctions = EnumHelper.EnumToList<ModuleFunctionType>().Where(e => e.Classification >= 1 && e.Classification <= 2).OrderBy(e => e.Classification).ToList();
        });

        public RelayCommand SaveCurrentModuleFunctionModelCommand => new RelayCommand(() =>
        {


            //ModulePoco current = new ModulePoco()
            //{
            //    //ParentId = TreeSelectItem.BillId,
            //    //moduleFunctionType = CurrentModuleFunctionModel.moduleFunctionType,
            //    ProjectId = Entity.BillId,
            //    PocoName = CurrentModuleModel.PocoName,
            //    Sort = Modules.Count,
            //    //CreateBy=SystemDataContext.GetInstance().loginUserInfo?.Name,

            //    CreateBy = "admin",
            //    CreateTime = DateTime.Now
            //};

            if (ValidationService.IsValidation(CurrentModuleModel))
            {
                try
                {
                    DC.Client.BeginTran();

                    //DC.AddEntity(current);
                    DC.Client.Storageable(CurrentModuleModel).ExecuteCommand();
                    Modules.Add(CurrentModuleModel);
                    DC.Client.CommitTran();
                    Dialog.ShowPopupWindow($"创建<{CurrentModuleModel.PocoName}> 成功！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Successful);
                    Show = false;
                }
                catch (Exception)
                {
                    DC.Client.RollbackTran();
                }
            }
            else
            {
                Show = false;
                Dialog.ShowPopupWindow($"{string.Join("\r\n", ValidationService.ErrorList)}", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);
            }




            ////Entity.ModuleFunction_Tree.Clear();
            //Entity.ModuleFunctionPocos.Add(current);
            ////Entity.BuildModuleFunctionTree(Guid.Empty.ToString());
            ////Entity.InsertChild(current);

            //DC.AddEntity(current);
            ////DC.SaveChanges();

            //Dialog.ShowPopupWindow($"创建<{current.moduleFunctionType.GetDisplay()}>-<{current.PocoName}> 成功！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Successful);

            //TreeSelectItem = null;
            //Show = false;

            //TreeSource = new System.Collections.ObjectModel.ObservableCollection<ModuleFunctionPoco>(Entity.ModuleFunction_Tree);
        });

        public RelayCommand<string> EditModuleCommand => new RelayCommand<string>((s) =>
        {
            bool IsOperation = false;
            if (s == "save")
            {
                DC.UpdateEntity(SelectedModule);
                IsOperation = true;
            }

            if (s == "delete")
            {
                DC.DeleteEntity(SelectedModule);
                IsOperation = true;
            }

            if (IsOperation)
            {
                LoadData();
            }

        });
        #endregion

        public RelayCommand CreateFunctionCommand => new RelayCommand(() =>
        {
            if (SelectedModule==null)
            {
                return;
            }

            CurrentFunctionModel = new FunctionPoco()
            {
                ProjectId = Entity.BillId,
                Sort = Modules.Count,
                BillId=SelectedModule.BillId,
            };

            Show2 = true;

        });

        public RelayCommand SaveNewFunctionCommand => new RelayCommand(() => { });
        #endregion
    }
}
